import { login, logout, getInfo, signUp, bindOauth } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'
import Login from '@/utils/login'

const getDefaultState = () => {
  return {
    token: getToken(),
    info: {},
    roles: []
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_INFO: (state, info) => {
    state.info = info
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  }
}

const actions = {
  // 登录
  login({ commit }, userInfo) {
    const { uname, pword } = userInfo
    const authenticationInfo = Login.generate(uname, pword)
    userInfo.authenticationInfo = authenticationInfo

    return new Promise((resolve, reject) => {
      login(userInfo).then(response => {
        const { param } = response
        commit('SET_TOKEN', param.token)
        setToken(param.token)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 重新获取用户信息
  getInfo({ commit }) {
    return new Promise((resolve, reject) => {
      getInfo().then(response => {
        const { user } = response.param
        if (!user) {
          reject('获取登录信息失败，请重新登录')
        }

        commit('SET_INFO', user)
        commit('SET_ROLES', user.permissions)

        resolve(user)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 登出
  logout({ commit }) {
    return new Promise((resolve, reject) => {
      logout().then(() => {
        removeToken()
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  signUp({ commit }, data) {
    const { uname, pword } = data
    const authenticationInfo = Login.generate(uname, pword)
    data.authenticationInfo = authenticationInfo

    return new Promise((resolve, reject) => {
      signUp(data).then(response => {
        const { param } = response
        if (param.token) {
          commit('SET_TOKEN', param.token)
          setToken(param.token)
        }
        resolve(param)
      }).catch(error => {
        reject(error)
      })
    })
  },

  bindOauth({ commit }, data) {
    const { uname, pword } = data
    const authenticationInfo = Login.generate(uname, pword)
    data.authenticationInfo = authenticationInfo

    return new Promise((resolve, reject) => {
      bindOauth(data).then(response => {
        const { param } = response
        if (param.token) {
          commit('SET_TOKEN', param.token)
          setToken(param.token)
        }
        resolve(param)
      }).catch(error => {
        reject(error)
      })
    })
  },

  updateInfo({ commit }, user) {
    commit('SET_INFO', user)
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken()
      commit('RESET_STATE')
      resolve()
    })
  },

  // check permission
  hasPermission({ state }, roles) {
    return state.roles.some(role => roles.includes(role))
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
